﻿using System;
using System.Net;
using System.Xml;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Unity;
using DXTools.UI.Core;
using DXTools.UI.Modules.JavaScriptConverter.Views;
using DXTools.UI.Core.Events;
using DXTools.UI.Core.ViewModel;


namespace DXTools.UI.Modules.JavaScriptConverter.ViewModels
{
    public class ToolbarViewModel : ViewModelBase
    {

        public DelegateCommand<object> ViewConverterCommand { get; private set; }
        public DelegateCommand<object> ConvertCommand { get; private set; }

        public ToolbarViewModel(IRegionManager regionManager, IEventAggregator eventAggregator, IUnityContainer container)
            : base(regionManager, eventAggregator, container)
        {
            this.HeaderInfo = "JavaScript Converter";
            this.ViewConverterCommand = new DelegateCommand<object>(this.ViewConverter, this.CanViewConverter);
            this.ConvertCommand = new DelegateCommand<object>(this.Convert, this.CanConvert);

            // View collection changed
            this.RegionManager.Regions["BodyRegion"].ActiveViews.CollectionChanged += (sender, e) =>
            {
                ConvertCommand.RaiseCanExecuteChanged();
            };

        }

        private void ViewConverter(object commandArg)
        {
            // Single View
            string viewName = typeof(JSConverterView).FullName;
            IRegion bodyRegion = RegionManager.Regions[RegionNames.BODY_REGION];
            object view = bodyRegion.GetView(viewName);
            if (view == null)
            {
                JSConverterView newView = UnityContainer.Resolve<JSConverterView>();
                bodyRegion.Add(newView, viewName);
                bodyRegion.Activate(newView);
                EventAggregator.GetEvent<ViewAddedEvent>().Publish(newView);
            }
            else
                bodyRegion.Activate(view);
         
        }

        private bool CanViewConverter(object commandArg)
        {
            return true;
        }

        private void Convert(object commandArg)
        {
             var singleView = this.RegionManager.Regions["BodyRegion"].ActiveViews.FirstOrDefault();
            if (singleView != null)
            {
                DXTools.UI.Core.View.ViewBase view = ((DXTools.UI.Core.View.ViewBase)singleView);

                if (view.GetType() == typeof(JSConverterView))
                {
                    this.EventAggregator.GetEvent<Events.JSConvertEvent>().Publish("");
                }
            }
        }
             

       

        private bool CanConvert(object commandArg)
        {
            bool canExport = false;

            var singleView = this.RegionManager.Regions["BodyRegion"].ActiveViews.FirstOrDefault();
            if (singleView != null)
            {
                DXTools.UI.Core.View.ViewBase view = ((DXTools.UI.Core.View.ViewBase)singleView);

                if (view.GetType() == typeof(JSConverterView))
                {
                    canExport = true;
                }
            }
           
            return canExport;
            

        }

         
    }
}
